<template>
  <s-modal
    title="添加"
    :visible="visible"
    width="800px"
    ok-text="保存"
    :confirm-loading="confirmLoading"
    :closable="!confirmLoading"
    @ok="save()"
    @cancel="
      () => {
        if (!confirmLoading) {
          visible = false
        }
      }
    "
  >
    <div style="height: 400px">
      <s-scroll>
        <s-form ref="form" :value="formValue">
          <s-form-item
            label="角色名称"
            name="text"
            :rules="[
              { whitespace: true, required: true, message: '角色名称不能为空' },
              { max: 50, message: '最长为50' }
            ]"
          >
            <s-input v-model="formValue.text" allow-clear placeholder="角色名称" />
          </s-form-item>
          <s-form-item
            label="角色标识"
            name="value"
            :rules="[
              { whitespace: true, required: true, message: '角色标识不能为空' },
              { max: 50, message: '最长为50' }
            ]"
          >
            <s-input v-model="formValue.value" allow-clear placeholder="角色标识" />
          </s-form-item>
          <s-form-item label="菜单" name="menu">
            <s-select
              v-model="formValue.menu"
              height="200px"
              url="/menu/template/selectList"
              value-field="id"
              label-field="templateName"
              placeholder="菜单"
            />
          </s-form-item>
          <s-form-item
            label="排序号"
            name="orderNum"
            :rules="[{ required: true, message: '排序号不能为空' }]"
          >
            <s-input-number v-model="formValue.orderNum" :min="1" :max="999" placeholder="排序号" />
          </s-form-item>
          <s-form-item
            label="备注"
            size="big"
            name="remark"
            :rules="[{ max: 200, message: '最长为200' }]"
          >
            <s-text-area v-model="formValue.remark" allow-clear placeholder="备注" />
          </s-form-item>
        </s-form>
      </s-scroll>
    </div>
  </s-modal>
</template>

<script>
export default {
  name: 'Add',

  data() {
    return {
      formValue: {},
      confirmLoading: false,
      visible: false
    }
  },
  methods: {
    open() {
      this.formValue = {}
      this.visible = true
    },
    save() {
      const form = this.$refs.form
      form.validate((values) => {
        this.confirmLoading = true
        this.$api
          .PUT('/authority/role/insert', values)
          .then((res) => {
            this.confirmLoading = false
            this.visible = false
            this.$emit('success')
            this.$message.info(res.msg)
          })
          .catch(() => {
            this.confirmLoading = false
          })
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
